<?php
class UploadFile{

    /**
     * @var string 文件名
     */
    private string $name;

    /**
     * @var string 文件类型
     */
    private string $type;

    /**
     * @var int 文件大小
     */
    private int $size;

    /**
     * @var int 上传时的错误代码
     */
    private int $error;

    /**
     * @var string 临时文件名
     */
    private string $tempName;

    public function __construct(array $file)
    {
        $this->name = $file['name'];
        $this->type = $file['type'];
        $this->size = $file['size'];
        $this->error = $file['error'];
        $this->tempName = $file['tmp_name'];
    }

    /**
     * 获取文件大小，单位MB
     * @return float
     */
    public function getSizeInMB():float
    {
        return round($this->size / 1024 / 1024, 2);
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function getSize(): int
    {
        return $this->size;
    }

    public function getError(): int
    {
        return $this->error;
    }

    public function getTempName(): string
    {
        return $this->tempName;
    }


    /**
     * 保存文件到某个目录或者某个文件中
     * @param string $path 保存的路径,如果是文件则保存到该文件中,当路径是文件时，newFileName和overwrite参数无效
     * @param string|null $newFileName 新文件名
     * @param string|null $extension 文件扩展名
     * @param bool $overwrite 是否覆盖已存在的文件
     * @return bool
     */
    public function saveFile(string $path, string $newFileName = null, string $extension = null, bool $overwrite = true):bool
    {
        if (is_dir($path)) {
            $newFileName = $newFileName ?? pathinfo($this->name, PATHINFO_FILENAME);
            $extension = $extension ?? pathinfo($this->name, PATHINFO_EXTENSION);
            $newFileName = $newFileName . '.' . $extension;
            $path = rtrim($path, '/') . '/' . $newFileName;
        }
        // 如果文件已存在,并且不允许覆盖,则返回false
        if (!$overwrite && file_exists($path)) {
            return false;
        }

        // 默认自动创建目录，如果存在默认覆盖
        return move_uploaded_file($this->tempName, $path);
    }



    /**
     * 对$_FILES数组中的数据转为正常的数组(一个元素一个文件)
     * @param array $files
     * @return array<string,UploadFile[]> 返回一个UploadFile数组
     */
    public static function parseUpLoadFiles(array $files):array
    {
        $res = [];
        foreach ($files as $key => $file) {
            $uploadName = $key;
            $uploadFiles = [];

            $names = $file['name'];
            $types = $file['type'];
            $tmpNames = $file['tmp_name'];
            $errors = $file['error'];
            $sizes = $file['size'];

            // 将多个文件的信息转为一个文件的信息
            foreach ($names as $index => $name) {
                $uploadFiles[] = [
                    'name' => $name,
                    'type' => $types[$index],
                    'tmp_name' => $tmpNames[$index],
                    'error' => $errors[$index],
                    'size' => $sizes[$index],
                ];
            }
            $res[$uploadName] = $uploadFiles;
        }

        $rr = [];
        // 将每个文件的信息转为UploadFile对象
        foreach ($res as $key => $file) {
            $uploadFiles = [];

//            // 如果只有一个文件,则直接转为UploadFile对象
//            if (count($file) === 1) {
//                $rr[$key] = new UploadFile($file[0]);
//                continue;
//            }

            foreach ($file as $item) {
                $uploadFiles[] = new UploadFile($item);
            }
            $rr[$key] = $uploadFiles;
        }
        return $rr;
    }

}